home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / chess / fics / telnet.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.5 KB  |  243 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4.  
  5. class Decoder:
  6.     '''
  7.     '''
  8.     buffer = ''
  9.     processing = False
  10.     
  11.     def __init__(self):
  12.         self.commands = {
  13.             240: (self.onEndSubnegotiation, False),
  14.             241: (self.onNoOp, False),
  15.             242: (self.onDataMark, False),
  16.             243: (self.onBreak, False),
  17.             244: (self.onInterruptProcess, False),
  18.             245: (self.onAbortOutput, False),
  19.             246: (self.onAreYouThere, False),
  20.             247: (self.onEraseCharacter, False),
  21.             248: (self.onEraseLine, False),
  22.             249: (self.onGoAhead, False),
  23.             250: (self.onStartSubnegotiation, False),
  24.             251: (self.onWill, True),
  25.             252: (self.onWont, True),
  26.             253: (self.onDo, True),
  27.             254: (self.onDont, True) }
  28.  
  29.     
  30.     def onData(self, data):
  31.         """Called when data from the telnet stream is decoded.
  32.         
  33.         'data' is the data inside the telnet stream (str).
  34.         """
  35.         pass
  36.  
  37.     
  38.     def onEndSubnegotiation(self):
  39.         pass
  40.  
  41.     
  42.     def onNoOp(self):
  43.         pass
  44.  
  45.     
  46.     def onDataMark(self):
  47.         pass
  48.  
  49.     
  50.     def onBreak(self):
  51.         pass
  52.  
  53.     
  54.     def onInterruptProcess(self):
  55.         pass
  56.  
  57.     
  58.     def onAbortOutput(self):
  59.         pass
  60.  
  61.     
  62.     def onAreYouThere(self):
  63.         pass
  64.  
  65.     
  66.     def onEraseCharacter(self):
  67.         pass
  68.  
  69.     
  70.     def onEraseLine(self):
  71.         pass
  72.  
  73.     
  74.     def onGoAhead(self):
  75.         pass
  76.  
  77.     
  78.     def onStartSubnegotiation(self):
  79.         pass
  80.  
  81.     
  82.     def onWill(self, option):
  83.         pass
  84.  
  85.     
  86.     def onWont(self, option):
  87.         pass
  88.  
  89.     
  90.     def onDo(self, option):
  91.         pass
  92.  
  93.     
  94.     def onDont(self, option):
  95.         pass
  96.  
  97.     
  98.     def onUnknownCommand(self, command):
  99.         pass
  100.  
  101.     
  102.     def registerIncomingData(self, data):
  103.         """Register data received from a telnet device.
  104.         
  105.         'data' is the received data (str).
  106.         """
  107.         if self.processing:
  108.             self.buffer += data
  109.             return None
  110.         self.processing = True
  111.         d = self.buffer + data
  112.         self.buffer = ''
  113.         while True:
  114.             d += self.buffer
  115.             if len(d) == 0:
  116.                 break
  117.             
  118.             index = d.find('\xff')
  119.             if index < 0:
  120.                 self.onData(d)
  121.                 break
  122.             
  123.             text = d[:index]
  124.             if len(text) > 0:
  125.                 self.onData(text)
  126.             
  127.             
  128.             try:
  129.                 command = ord(d[index + 1])
  130.             except IndexError:
  131.                 self.buffer = d[index:]
  132.                 break
  133.  
  134.             if command == 255:
  135.                 self.onData('\xff')
  136.                 d = d[index + 2:]
  137.                 continue
  138.             
  139.             
  140.             try:
  141.                 (method, hasParameter) = self.commands[command]
  142.             except KeyError:
  143.                 self.onUnknownCommand(command)
  144.                 d = d[index + 2:]
  145.                 continue
  146.  
  147.             if hasParameter:
  148.                 
  149.                 try:
  150.                     parameter = ord(d[index + 2])
  151.                 except IndexError:
  152.                     self.buffer = d[index:]
  153.                     break
  154.  
  155.                 method(parameter)
  156.                 d = d[index + 3:]
  157.                 continue
  158.             method()
  159.             d = d[index + 2:]
  160.         self.processing = False
  161.  
  162.  
  163. if __name__ == '__main__':
  164.     
  165.     class D(Decoder):
  166.         
  167.         def onData(self, data):
  168.             print 'onData(' + repr(data) + ')'
  169.  
  170.         
  171.         def onEndSubnegotiation(self):
  172.             print 'onEndSubnegotiation()'
  173.  
  174.         
  175.         def onNoOp(self):
  176.             print 'onNoOp()'
  177.  
  178.         
  179.         def onDataMark(self):
  180.             print 'onDataMark()'
  181.  
  182.         
  183.         def onBreak(self):
  184.             print 'onBreak()'
  185.  
  186.         
  187.         def onInterruptProcess(self):
  188.             print 'onInterruptProcess()'
  189.  
  190.         
  191.         def onAbortOutput(self):
  192.             print 'onAbortOutput()'
  193.  
  194.         
  195.         def onAreYouThere(self):
  196.             print 'onAreYouThere()'
  197.  
  198.         
  199.         def onEraseCharacter(self):
  200.             print 'onEraseCharacter()'
  201.  
  202.         
  203.         def onEraseLine(self):
  204.             print 'onEraseLine()'
  205.  
  206.         
  207.         def onGoAhead(self):
  208.             print 'onGoAhead()'
  209.  
  210.         
  211.         def onStartSubnegotiation(self):
  212.             print 'onStartSubnegotiation()'
  213.  
  214.         
  215.         def onWill(self, option):
  216.             print 'onWill(%i)' % option
  217.  
  218.         
  219.         def onWont(self, option):
  220.             print 'onWont(%i)' % option
  221.  
  222.         
  223.         def onDo(self, option):
  224.             print 'onDo(%i)' % option
  225.  
  226.         
  227.         def onDont(self, option):
  228.             print 'onDont(%i)' % option
  229.  
  230.         
  231.         def onUnknownCommand(self, command):
  232.             print 'onUnknownCommand(%i)' % command
  233.  
  234.  
  235.     d = D()
  236.     d.registerIncomingData('A telnet\xff\xff string \xff\xf3 with a break and a DO 6 \xff\xfd\x06. Fun innit?')
  237.     d.registerIncomingData('ABC\xff')
  238.     d.registerIncomingData('\xf3DEF')
  239.     d.registerIncomingData('ABC\xff')
  240.     d.registerIncomingData('\xfd')
  241.     d.registerIncomingData('\nDEF')
  242.  
  243.